package com.xl6667.controller;

import cn.hutool.core.lang.Validator;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.entity.User;
import com.xl6667.enums.AppHttpCodeEnum;
import com.xl6667.exception.SystemException;
import com.xl6667.service.BlogLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
@Api(tags = "博客登录模块")
public class BlogLoginController {
    @Autowired
    private BlogLoginService blogLoginService;
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ResponseResult login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        return blogLoginService.login(user);

    }
    @PostMapping("/login/sms")
    @ApiOperation(value = "短信登录")
    public ResponseResult loginSms(String phonenumber,String code){
        if(!Validator.isMobile(phonenumber)){
            throw new SystemException(AppHttpCodeEnum.PHONENUMBER_NO);
        }
        return blogLoginService.loginSms(phonenumber,code);

    }
    @PostMapping("/sms")
    @ApiOperation(value = "获取短信验证码")
    public ResponseResult getSms( String phonenumber){
        if (!Validator.isMobile(phonenumber)){
            throw new SystemException(AppHttpCodeEnum.PHONENUMBER_NO);
        }
        return blogLoginService.getSms(phonenumber);
    }
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public ResponseResult logout(){
        return blogLoginService.logout();
    }

}
